<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <el-card>
      <el-input></el-input><el-button>搜索</el-button
      ><el-button type="primary" @click="tiao">添加商品</el-button> <br />
      <br />
      <el-table border style="width: 100%" :data="tableData">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | datatime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">编辑</el-button>
            <el-button type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="user.pagenum"
        :page-sizes="[15, 20, 30, 40]"
        :page-size="user.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getgoodsApi } from "@/http/api";
export default {
  data() {
    return {
      tableData: [], //渲染的列表
      total: 0, //商品总条数
      user: {
        //要传递的值
        query: "",
        pagenum: 1, //当前页数
        pagesize: 15, //当前渲染条数
      },
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
      //点击添加跳转页面
      tiao(){
          this.$router.push('/add') 
      },
    handleSizeChange(val) {
      this.user.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.user.pagenum = val;
      this.getlist();
    },
    async getlist() {
      let res = await getgoodsApi(this.user);
      console.log(res);
      let goods = res.goods;
      let total = res.total;
      this.tableData = goods;
      this.total = total;
    },
  },
};
</script>

<style scoped>
.el-input {
  width: 350px;
}
</style>
